package yz.dut.edu.graduate.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yz.dut.edu.graduate.bean.Result;
import yz.dut.edu.graduate.dao.HistoryMapper;
import yz.dut.edu.graduate.pojo.Dish;
import yz.dut.edu.graduate.pojo.History;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HistoryService {
    @Autowired
    HistoryMapper historyMapper;

    public Result InsertHistory(String position, Date date,String remark,float order_price){
        Result result=new Result();
        History history=new History();
        history.sethId(date.getTime());
        history.sethDate(date);
        history.setRemark(remark);
        history.setOrderPrice(order_price);
        history.setPosition(position);
        try{
            historyMapper.insertSelective(history);
            result.setSuccess(true);
            result.setMsg("下单成功，请使用微信支付交款");
            result.setDetails(history.gethId());
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public Result AffirmCheck(long h_id,float real_price){
        Result result=new Result();
        History history=new History();
        history.sethId(h_id);
        history.setRealPrice(real_price);
        history.setState(1);
        try{
            History temp=historyMapper.selectByPrimaryKey(h_id);
            if(temp==null){
                result.setMsg("不存在此订单");
                return result;
            }
            if(temp.getOrderPrice()<real_price){
                result.setMsg("账单金额小于实收金额，请检查是否输入错误");
                return result;
            }
            if(temp.getState()!=0){
                result.setMsg("账单已经支付完成，请勿重复支付");
                return result;
            }
            result.setDetails(historyMapper.updateByPrimaryKeySelective(history));
            if(0!=(int)result.getDetails()){
                result.setSuccess(true);
                //返回值为String
                String res = String.format("%.2f",temp.getOrderPrice()-real_price);
                result.setMsg("更新状态成功，已向后厨通知做菜，已抹零"+res+"元");
            }else{
                result.setMsg("更新状态失败，原因可能是订单号错误");
            }
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public PageInfo<History> SelectPage(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<History> res=historyMapper.selectPage();
        PageInfo<History> pageInfo=new PageInfo<>(res);
        return pageInfo;
    }

    public Result SearchRange(String start, String end){
        String sql="select * from history\n" +
                "where date(h_date) between '"+start+"' and '"+end+"';";
        Result result=new Result();
        try {
            List<Map<String,Object>> res=historyMapper.customizedSql(sql);
            result.setDetails(res);
            result.setSuccess(true);
            result.setMsg("成功");
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public Result SelectHistoryDetailsByHid(long h_id){
        String sql=new String("select dish.dish_name,history_details.quantity from history join(\n" +
                "\tdish join history_details on dish.d_id=history_details.d_id)\n" +
                "on history.h_id=history_details.h_id\n" +
                "where history.h_id=");
        sql+=(h_id+";");
        Result result=new Result();
        try {
            result.setDetails(historyMapper.customizedSql(sql));
            result.setSuccess(true);
            result.setMsg("成功");
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public Result UpdateState(long h_id){
        Result result=new Result();
        History history=new History();
        history.sethId(h_id);
        history.setState(2);
        try {
            result.setDetails(historyMapper.updateByPrimaryKeySelective(history));
            result.setSuccess(true);
            result.setMsg("完成成功");
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public Result GetData(){
        String sql="select history.h_id,history.position,dish.dish_name,history_details.quantity,history.remark from history join(\n" +
                "\tdish join history_details on dish.d_id=history_details.d_id)\n" +
                "on history.h_id=history_details.h_id\n" +
                "where history.state=1;";
        Result result=new Result();
        try {
            List<Map<String,Object>> temp=historyMapper.customizedSql(sql);
            Map<Long,Map<String,String>> res=new HashMap<>();
            for(Map<String,Object> item:temp){
                long id=(Long)item.get("h_id");
                String dishname=(String)item.get("dish_name");
                int cnt=(Integer)item.get("quantity");
                if(res.containsKey(id)){
                    String oldDishName=res.get(id).get("dishname");
                    res.get(id).put("dishname",oldDishName+" "+dishname+cnt+"份");
                }else{
                    String pos=(String)item.get("position");
                    String remark=(String)item.get("remark");
                    Map<String,String> subRes=new HashMap<>();
                    subRes.put("position",pos);
                    subRes.put("remark",remark);
                    subRes.put("dishname",dishname+cnt+"份");
                    res.put(id,subRes);
                }
            }
            result.setDetails(res);
            result.setSuccess(true);
            result.setMsg("成功");
        }catch (Exception e){
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
